//
//  AppDelegate.m
//  Food
//
//  Created by dlios on 15-7-13.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "AppDelegate.h"
#import "NearFoodViewController.h"
#import "HomePageViewController.h"
#import "HealthEatViewController.h"
#import "MenuViewController.h"
#import "LeftViewController.h"

#import <ShareSDK/ShareSDK.h>
#import <ShareSDKConnector/ShareSDKConnector.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import "WXApi.h"
#import "WeiboSDK.h"


@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate



+ (NSInteger)OSVersion
{
    static NSUInteger _deviceSystemMajorVersion = -1;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _deviceSystemMajorVersion = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue];
    });
    return _deviceSystemMajorVersion;
}

- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];
    
    [ShareSDK registerApp:@"8f144ec59e76"
          activePlatforms:@[@(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeTencentWeibo), @(SSDKPlatformTypeFacebook), @(SSDKPlatformTypeTwitter), @(SSDKPlatformTypeWechat), @(SSDKPlatformTypeQQ)]
                 onImport:^(SSDKPlatformType platformType) {
                     
                     switch (platformType)
                     {
                         case SSDKPlatformTypeWechat:
                             [ShareSDKConnector connectWeChat:[WXApi class]];
                             break;
                         case SSDKPlatformTypeQQ:
                             [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                             break;
                         default:
                             break;
                     }
                     
                 }
          onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) {
              
              switch (platformType)
              {
                  case SSDKPlatformTypeSinaWeibo:
                      //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                      [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"
                                                appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                              redirectUri:@"http://www.sharesdk.cn"
                                                 authType:SSDKAuthTypeBoth];
                      break;
                  case SSDKPlatformTypeTencentWeibo:
                      //设置腾讯微博应用信息，其中authType设置为只用Web形式授权
                      [appInfo SSDKSetupTencentWeiboByAppKey:@"801307650"
                                                   appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
                                                 redirectUri:@"http://www.sharesdk.cn"];
                      break;
                  case SSDKPlatformTypeFacebook:
                      //设置Facebook应用信息，其中authType设置为只用SSO形式授权
                      [appInfo SSDKSetupFacebookByAppKey:@"107704292745179"
                                               appSecret:@"38053202e1a5fe26c80c753071f0b573"
                                                authType:SSDKAuthTypeBoth];
                      break;
                  case SSDKPlatformTypeTwitter:
                      [appInfo SSDKSetupTwitterByConsumerKey:@"LRBM0H75rWrU9gNHvlEAA2aOy"
                                              consumerSecret:@"gbeWsZvA9ELJSdoBzJ5oLKX0TU09UOwrzdGfo9Tg7DjyGuMe8G"
                                                 redirectUri:@"http://mob.com"];
                      break;
                  case SSDKPlatformTypeWechat:
                      [appInfo SSDKSetupWeChatByAppId:@"wx4868b35061f87885"
                                            appSecret:@"64020361b8ec4c99936c0e3999a9f249"];
                      break;
                  case SSDKPlatformTypeQQ:
                      [appInfo SSDKSetupQQByAppId:@"100371282"
                                           appKey:@"aed9b0303e3ed1e27bae87c33761161d"
                                         authType:SSDKAuthTypeSSO];
                      break;
                  default:
                      break;
              }
              
          }];

    
    
    
    
    UITabBarController *tabBC = [[UITabBarController alloc] init];
    tabBC.delegate = self;
    self.window.rootViewController = tabBC;

   [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor],UITextAttributeTextColor, nil] forState:UIControlStateNormal];


   [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:REDCOLOR(1),UITextAttributeTextColor, nil] forState:UIControlStateSelected];
    
    HomePageViewController *homePageVC = [[HomePageViewController alloc] init];
    UINavigationController *naviVC1 = [[UINavigationController alloc] initWithRootViewController:homePageVC];
     homePageVC.tabBarController.tabBar.tintColor = [UIColor redColor];
     homePageVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"HomePage2"] selectedImage:[[UIImage imageNamed:@"HomePage1"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
       homePageVC.title = @"首页";



    MenuViewController *leanCookingVC = [[MenuViewController alloc] init];
     leanCookingVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"学做菜" image:[UIImage imageNamed:@"food2"] selectedImage:[[UIImage imageNamed:@"food"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    UINavigationController *naviVC2 = [[UINavigationController alloc] initWithRootViewController:leanCookingVC];
    

    HealthEatViewController *healthVC = [[HealthEatViewController alloc] init];
    healthVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"健康饮食" image:[UIImage imageNamed:@"health2"] selectedImage:[[UIImage imageNamed:@"health"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    UINavigationController *naviVC3 = [[UINavigationController alloc] initWithRootViewController:healthVC];

    NearFoodViewController *nearVC = [[NearFoodViewController alloc] init];
    nearVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"附近美食" image:[UIImage imageNamed:@"near2"] selectedImage:[[UIImage imageNamed:@"near"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    UINavigationController *naviVC4 = [[UINavigationController alloc] initWithRootViewController:nearVC];
    
    tabBC.viewControllers = @[naviVC1, naviVC2, naviVC3, naviVC4];
    
    [tabBC release];
    [homePageVC release];
    [naviVC1 release];
    [leanCookingVC release];
    [naviVC2 release];
    [healthVC release];
    [naviVC3 release];
    [nearVC release];
    [naviVC4 release];
    AppDelegate *app = [UIApplication sharedApplication].delegate;
    app.rootTBC = tabBC;
    
    
    return YES;
}

#warning mark- 点击tabbar 刷新viewController
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if (tabBarController.selectedIndex == 2) {
        NSLog(@"刷新");
        UINavigationController *navi = (UINavigationController *)viewController;
        HealthEatViewController *eat = (HealthEatViewController *)navi.topViewController;
        [eat data];
    }
    
}




- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
    
}


@end
